package com.designpattern.prototype;


import java.io.*;

/**
 * 项目名称: java_algorithm
 * @ClassName PrototypeTest
 * @Description
 * @Author huipanxing huipanxing@chinasofti.com   @Date 2021/9/8 15:09
 */
public class PrototypeTest {

    public static void main(String[] args) {
        BaseInfo baseInfo = new BaseInfo("小沫沫");
        Product product = new Product("prat1", "part2", 3, 4, baseInfo);
        Product clone = product.clone();
        clone.getBaseInfo().setCompanyName("小灰灰");
        System.out.println(product);
        System.out.println(clone);
    }

}

class BaseInfo implements Cloneable,Serializable {
    private String companyName;

    public BaseInfo(String companyName) {
        this.companyName = companyName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    @Override
    protected BaseInfo clone() throws CloneNotSupportedException {
        return (BaseInfo) super.clone();
    }

    @Override
    public String toString() {
        return "BaseInfo{" +
                "companyName='" + companyName + '\'' +
                '}';
    }
}

class Product implements Cloneable,Serializable {

    private String part1;

    private String part2;

    private Integer part3;

    private Integer part4;

    private BaseInfo baseInfo;

    public Product(String part1, String part2, Integer part3, Integer part4, BaseInfo baseInfo) {
        this.part1 = part1;
        this.part2 = part2;
        this.part3 = part3;
        this.part4 = part4;
        this.baseInfo = baseInfo;
    }

    public String getPart1() {
        return part1;
    }

    public void setPart1(String part1) {
        this.part1 = part1;
    }

    public String getPart2() {
        return part2;
    }

    public void setPart2(String part2) {
        this.part2 = part2;
    }

    public Integer getPart3() {
        return part3;
    }

    public void setPart3(Integer part3) {
        this.part3 = part3;
    }

    public Integer getPart4() {
        return part4;
    }

    public void setPart4(Integer part4) {
        this.part4 = part4;
    }

    public BaseInfo getBaseInfo() {
        return baseInfo;
    }

    public void setBaseInfo(BaseInfo baseInfo) {
        this.baseInfo = baseInfo;
    }

    @Override
    protected Product clone() {
//        try {
//            Product product=(Product) super.clone();
//            BaseInfo baseInfo = product.getBaseInfo().clone();
//            product.setBaseInfo(baseInfo);
//            return product;
//        } catch (CloneNotSupportedException e) {
//            e.printStackTrace();
//        }

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try (ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream)) {

            oos.writeObject(this);

        } catch (IOException e) {
            e.printStackTrace();
        }
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        try (ObjectInputStream ois = new ObjectInputStream(byteArrayInputStream)) {

            Product product = (Product) ois.readObject();
            return product;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    public String toString() {
        return "Product{" +
                "part1='" + part1 + '\'' +
                ", part2='" + part2 + '\'' +
                ", part3=" + part3 +
                ", part4=" + part4 +
                ", baseInfo=" + baseInfo +
                '}';
    }
}